package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.OrderListBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.MathUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class MyOrderListAdapter extends RecyclerArrayAdapter<OrderListBean.DataBean> {

    public interface myInterFace {
        void leftClick(int position);

        void rightClick(int position);

        void OneClick(int position);

        void goods(int position);
    }

    private myInterFace myInterfaceListener;

    public void setMyInterfaceListener(myInterFace listener) {
        myInterfaceListener = listener;

    }


    public MyOrderListAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyOrderHolder(parent);
    }

    class MyOrderHolder extends BaseViewHolder<OrderListBean.DataBean> {

        @Bind(R.id.img_business)
        ImageView imgBusiness;
        @Bind(R.id.tv_business)
        TextView tvBusiness;
        @Bind(R.id.tv_order_status)
        TextView tvOrderStatus;
        @Bind(R.id.img_goods)
        ImageView imgGoods;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.tv_goods_type)
        TextView tvGoodsType;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.goods_layout)
        RelativeLayout goodsLayout;
        @Bind(R.id.tv_goods_price)
        SuperTextView tvGoodsPrice;
        @Bind(R.id.tv_integral_deduction)
        SuperTextView tvIntegralDeduction;
        @Bind(R.id.tv_freight)
        SuperTextView tvFreight;
        @Bind(R.id.tv_total_price)
        SuperTextView tvTotalPrice;
        @Bind(R.id.btn_one)
        SuperButton btnOne;
        @Bind(R.id.btn_two)
        SuperButton btnTwo;
        @Bind(R.id.btn_san)
        SuperButton btnSan;
        @Bind(R.id.tv_goods_number)
        SuperTextView tvGoodsNumber;

        public MyOrderHolder(ViewGroup parent) {
            super(parent, R.layout.item_my_order);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(OrderListBean.DataBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.merchant_icon)
                    .apply(MyApplication.options_circle)
                    .into(imgBusiness);
            Glide.with(BaseActivity.context)
                    .load(data.product_icon)
                    .apply(MyApplication.options)
                    .into(imgGoods);
            tvBusiness.setText(data.merchant);
            tvOrderStatus.setText(data.status_str);
            tvGoodsNumber.setRightString(data.code);
            switch (data.status) {
                case 1://未付款

                    btnOne.setVisibility(View.GONE);

                    btnTwo.setText("取消订单");
                    btnTwo.setVisibility(View.VISIBLE);
                    btnSan.setText("去支付");
                    btnSan.setVisibility(View.VISIBLE);
                    break;
                case 2://已付款
                    btnTwo.setVisibility(View.VISIBLE);
                    btnOne.setVisibility(View.GONE);
                    if (data.service_status == 1) {
                        btnTwo.setText("我要退款");
                        btnSan.setVisibility(View.GONE);

                    } else if (data.service_status == 7) {
                        btnTwo.setText("查询退款进度");
                        btnSan.setVisibility(View.VISIBLE);
                        btnSan.setText("删除订单");

                    } else {
                        btnTwo.setText("查询退款进度");
                        btnSan.setVisibility(View.GONE);

                    }


                    break;
                case 3://已发货
                    btnOne.setVisibility(View.GONE);
                    btnTwo.setText("查询物流");
                    btnTwo.setVisibility(View.VISIBLE);
                    btnSan.setText("确认收货");
                    btnSan.setVisibility(View.VISIBLE);
                    break;
                case 4://已收货
                    btnTwo.setVisibility(View.VISIBLE);
                    btnOne.setVisibility(View.VISIBLE);

                    if (data.service_status == 12 || data.service_status == 13 || data.service_status == 15 || data.service_status == 16 || data.service_status == 17) {
                        btnOne.setText("查询换货进度");
                        btnTwo.setVisibility(View.GONE);
                    } else if (data.service_status == 14) {//同意换货
                        btnOne.setText("填写换货信息");
                        btnTwo.setVisibility(View.GONE);
                    } else if (data.service_status == 1) {
                        btnOne.setText("我要换货");
                    }

                    if (data.service_status == 2 || data.service_status == 3 || data.service_status == 5 || data.service_status == 6 || data.service_status == 7) {
                        btnOne.setVisibility(View.GONE);
                        btnTwo.setText("查询退款进度");
                    } else if (data.service_status == 4) {//同意退款
                        btnTwo.setText("填写退货信息");
                        btnOne.setVisibility(View.GONE);
                    } else if (data.service_status == 1) {
                        btnTwo.setText("我要退货");
                    }

                    btnSan.setVisibility(View.VISIBLE);
                    btnSan.setText("删除订单");


                    break;
                case 5://交易已关闭
                    btnOne.setVisibility(View.GONE);
                    btnTwo.setVisibility(View.GONE);
                    if (data.service_status == 7) {
                        btnTwo.setVisibility(View.VISIBLE);
                        btnTwo.setText("查询退款进度");
                        btnSan.setVisibility(View.VISIBLE);
                        btnSan.setText("删除订单");

                    } else if (data.service_status == 17) {
                        btnTwo.setVisibility(View.VISIBLE);
                        btnTwo.setText("查询换货进度");
                        btnSan.setVisibility(View.VISIBLE);
                        btnSan.setText("删除订单");
                    } else {
                        btnTwo.setVisibility(View.GONE);
                        btnSan.setVisibility(View.VISIBLE);
                        btnSan.setText("删除订单");
                    }

                    break;
            }

            tvGoodsName.setText(data.product_name);
            tvGoodsType.setText(data.product_option.replaceAll("，", ",").replaceAll("（", "(").replaceAll("）", ")").replaceAll(" ", "") + "");
            tvNum.setText("x " + data.quantity);
            tvGoodsPrice.setRightString("￥" + MathUtil.trimShu(data.product_amount + "")).useShape();
            tvIntegralDeduction.setRightString("- ￥0").useShape();
            tvFreight.setRightString("￥" + MathUtil.trimShu(data.freight)).useShape();
            tvTotalPrice.setRightString("￥" + MathUtil.trimShu(data.amount + "")).useShape();


            btnTwo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myInterfaceListener != null) {
                        myInterfaceListener.leftClick(getDataPosition());
                    }
                }
            });
            btnSan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myInterfaceListener != null) {
                        myInterfaceListener.rightClick(getDataPosition());
                    }
                }
            });
            goodsLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myInterfaceListener != null) {
                        myInterfaceListener.goods(getDataPosition());
                    }
                }
            });

            btnOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (myInterfaceListener != null) {
                        myInterfaceListener.OneClick(getDataPosition());
                    }
                }
            });
        }
    }

}
